<template>
  <q-dialog v-model="show" persistent>
    <q-card v-drag class="dialog-content" style="min-width: 700px; max-height: 550px;overflow-y: hidden">
      <q-card-section class="dialog-title bg-primary" >
        <div class="text-center text-white dialog-title">
          <span class="font-size-16">新增原因<span style="font-size: 12px">（一次最多增加10条）</span></span>
<!--          <q-btn-->
<!--            style="font-size: 18px;color: white"-->
<!--            flat round dense-->
<!--            class="iconfont icontubiao-50 white close-btn dialog"-->
<!--            @click="addRows" :disable="addList.length == 10"-->
<!--          />-->
        </div>
      </q-card-section>
      <q-form  @submit="submit" greedy>
        <q-card style="margin: 20px 20px 0; box-shadow: none">
          <q-card-section class="dialog-title" style="background-color: #F3F3F3">
            <div class="text-center dialog-title" style="padding-left: 0; text-align: left" >
              <span class="font-size-16">{{groupName? '原因类型:' + groupName : '让步原因'}}</span>
              <q-btn
                style="font-size: 18px; color: white"
                flat
                round
                dense
                class="iconfont icontubiao-39 text-primary close-btn dialog"
                @click="addRows" :disable="addList.length === 10"
              />
            </div>
          </q-card-section>
        </q-card>
        <div class="table-list scroll-box" :style="`height: ${addList.length * 72 + 5}px`">
<!--              <div class="form-inline" style="padding: 20px 20px 0;" v-if="groupName">-->
<!--                <div class="q-field row no-wrap items-start q-input q-field&#45;&#45;outlined q-field&#45;&#45;float q-field&#45;&#45;dense q-field&#45;&#45;with-bottom items-center" style="padding-bottom: 0">-->
<!--                  <div class="q-field__before q-field__marginal row no-wrap items-center">-->
<!--                    <label class="text-right name">-->
<!--                      <span>原因类型:</span>-->
<!--                    </label>-->
<!--                  </div>-->
<!--                  <div>{{groupName}}</div>-->
<!--                </div>-->
<!--              </div>-->
          <div class="form-inline border-b" style="padding-bottom: 0" v-for="(item, index) in addList" :key="index">
                <div class="items-center flex full-width">
                  <div style="width: calc(100% - 36px)" class="margin-t-20 flex">
                    <yz-input
                      :rules="[0, 11]"
                      :hint-rule="[val => !repeat('code', index, 'item') || '原因代码不可重复']"
                      v-model.trim="item.code"
                      :maxlength="20"
                      title="原因代码"
                      class="margin-r-20"
                    />
                    <yz-input
                      :rules="[0]"
                      input-type="textarea"
                      :autogrow="false"
                      :hint-rule="[val => !repeat('label', index, 'item') || '原因名称不可重复']"
                      v-model.trim="item.label"
                      :maxlength="50"
                      title="原因名称"
                    />
                  </div>
                  <table-btn btn-type="delete" :disable="addList.length === 1" @click="onDeleteClick(index)"/>
<!--                  <q-btn-->
<!--                    flat round dense-->
<!--                    icon="iconfont icontubiao-16"-->
<!--                    color="primary"-->
<!--                    @click="onDeleteClick(index)"-->
<!--                    style="min-width: 32px;width: 32px;"-->
<!--                    class="margin-l-20"-->
<!--                    :disable="addList.length == 1"-->
<!--                  >-->
<!--                    <q-tooltip>删除</q-tooltip>-->
<!--                  </q-btn>-->
              </div>
              </div>
        </div>
        <!-- 底部按钮 -->
        <q-separator />
        <div class="dialog-footer text-center margin-t-10">
          <q-btn outline label="取消" color="primary" @click="$emit('close')" class="q-mr-md"/>
          <q-btn unelevated label="确认" color="primary" type="submit"/>
        </div>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<script>
export default {
  name: 'batch-add-reason',
  props: {
    show: {
      type: Boolean,
      default: false,
      required: true
    },
    groupName: {
      default: '',
      required: true
    }
  },
  data () {
    return {
      thumbStyle: this.CommonConfig.thumbStyle, // 滚动条样式
      addList: [
        {
          code: '',
          label: '',
          enable: true
        }
      ],
      tableColumns: [
        { name: 'indexId', label: '序号', field: 'indexId', align: 'left' },
        { name: 'code', label: '原因代码', field: 'code', align: 'left' },
        { name: 'label', label: '原因', field: 'label', align: 'left' },
        { name: 'operate', label: '操作', field: 'operate', align: 'center' }
      ]
    }
  },
  methods: {
    // 添加行
    addRows () {
      const item = {
        code: '',
        label: '',
        enable: true
      }
      this.addList.push(item)
    },
    // 判重
    repeat (key, i, type) {
      let isRepeat = false
      if (type === 'item') {
        const item = this.addList[i]
        const flag = item[key]
        this.addList.forEach((item, index) => {
          if (item[key] === flag && i !== index) {
            isRepeat = true
            return false
          }
        })
      } else {
        let array = []
        this.addList.forEach(item => {
          if (array.length > 0) {
            if (array.indexOf(item[key]) === -1) {
              array.push(item[key])
            } else {
              isRepeat = true
              return false
            }
          } else {
            array.push(item[key])
          }
        })
      }
      return isRepeat
    },
    // 删除
    onDeleteClick (index) {
      this.addList.splice(index, 1)
    },
    // 提交
    submit () {
      let params = []
      this.addList.forEach(item => {
        params.push(item)
      })
      this.$emit('batchAddReason', params)
    }
  },
  watch: {
    show: function () {
      this.addList = [{
        code: '',
        label: '',
        enable: true
      }]
    }
  }
}
</script>

<style scoped lang="stylus">
  .border-b{
    border-bottom $border
  }
  .border-b.form-inline:last-child{
    border-bottom 0
  }
  >>>.textarea{
    .q-field__control{
      min-height 32px !important
      max-height 32px !important
    }
    textarea {
      resize none !important
      overflow hidden !important
    }
  }
    .table-list{
      max-height: 220px;
      margin: 0 20px 20px;
      border: $border;
      display: flex;
      flex-direction: column;
      border-top 0
      border-bottom-left-radius: 6px;
      border-bottom-right-radius: 6px;
    }
      .dialog-title{
        border-bottom-left-radius: 0 !important;
        border-bottom-right-radius: 0 !important;
      }
  /*.form-inline:nth-child(even) {*/
  /*  background-color: #f9f9f9;*/
  /*}*/
</style>
